#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "OSSRV_SOCKETS_Linux.h"
#include "../COMMON/common.h"

#ifdef VXWORKS
#include <memLib.h>
#else
#include <memory.h>
#include <malloc.h>/* don't delete - improves linux compilation */
#endif


int OSSRV_SOCKETS_accept (int socket, struct sockaddr *addr, socklen_t *addrlen)
{
    return (accept(socket, addr, addrlen));
}
  

int OSSRV_SOCKETS_bind (int socket, const struct sockaddr *name, socklen_t namelen)
{
    return (bind(socket, name, namelen));
}

int OSSRV_SOCKETS_close (int  socket)
{
	int result;
	result = close(socket);
	if (result < 0)
	{
		//PASLOG_ERROR_1(PASLOG_FUNC_GENERAL, PASLOG_OLT_IRRELEVANT, PASLOG_ONU_IRRELEVANT, "Failed to _close file descriptor %d\n",socket);
	}
	return result;
}

int OSSRV_SOCKETS_connect (int socket ,const struct sockaddr *name, socklen_t namelen)
{
    return (connect(socket,name,namelen));
}


int OSSRV_SOCKETS_fcntl( int  socket , int  cmd , long  arg )
{
	return (fcntl(socket, cmd, arg));
}


int OSSRV_SOCKETS_get_host_addr(const char *name, struct sockaddr_in *address)
{
    	struct hostent *hp;          /* sddress of the destination machine */
	hp = (struct hostent *)gethostbyname(name);
	if( hp == NULL)    
		return EXIT_ERROR;
	else
		memcpy(&(address->sin_addr.s_addr), hp->h_addr, hp->h_length);

	return EXIT_OK;
}

unsigned long OSSRV_SOCKETS_inet_addr(const char *ip_address_str)
{
	return inet_addr(ip_address_str);
}

int OSSRV_SOCKETS_get_sock_name (int socket, struct sockaddr *name, socklen_t *namelen)
{
    return (getsockname(socket,name,namelen));
}


int OSSRV_SOCKETS_ioctl (int socket,int cmd, void *val)
{
	return (ioctl(socket, cmd, val));
}


int OSSRV_SOCKETS_read (int  socket, void *buf, size_t len)
{
	int nbytes;

	nbytes = read(socket, buf, len);

	return nbytes;
}


int OSSRV_SOCKETS_socket (int socket_family, int type, int protocol)
{
    return (socket(socket_family,type,protocol));
}

int OSSRV_SOCKETS_send_to (int socket, const void *msg, size_t len, unsigned int flags,
                           const struct sockaddr *to, socklen_t tolen)
{
    return (sendto(socket,msg,len,flags,to,tolen));
}


int OSSRV_SOCKETS_select (int nfds, fd_set *readset, fd_set *writeset, fd_set *exceptset,
                          struct timeval *timeout)
{
    return (select(nfds,readset,writeset,exceptset,timeout));
}

int OSSRV_SOCKETS_recv (int socket, void *buf, int len, unsigned int flags)
{
    return (recv(socket,buf,len,flags));
}

int OSSRV_SOCKETS_recv_from (int socket, void *buf, int len, unsigned int flags,
                             struct sockaddr *from, socklen_t *fromlen)
{
    return (recvfrom(socket,buf,len,flags,from,fromlen));
}

unsigned int        OSSRV_SOCKETS_htonl (unsigned int hostlong)
{
    return (htonl(hostlong));
}

unsigned short int  OSSRV_SOCKETS_htons (unsigned short int hostshort)
{
    return (htons(hostshort));
}


unsigned short int  OSSRV_SOCKETS_ntohs (unsigned short int netshort)
{
    return (ntohs(netshort));
}

